package com.dx.mes.service.query.service;
import static com.dx.mes.common.bean.def.GenericDef.E_BIS_MTRL;
import static com.dx.mes.common.bean.def.GenericDef.E_FIPWOMTRL_INVALID_INPUT;
import static com.dx.mes.common.bean.def.GenericDef.E_READ_NOT_FOUND;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_MESG_OK;
import static com.dx.mes.common.bean.def.GenericDef.TRX_OUT;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.common.table.ppt.Bis_mtrl;
import com.dx.common.table.ppt.Ret_wo_mtrl;
import com.dx.mes.common.dao.IBisMtrlDao;
import com.dx.mes.common.dao.IRetWoMtrlDao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.query.service.IMipwomtrlService;
import com.dx.mes.facade.query.tx.Mipwomtrl.MipwomtrlI;
import com.dx.mes.facade.query.tx.Mipwomtrl.MipwomtrlO;
import com.dx.mes.facade.query.tx.Mipwomtrl.MipwomtrlOA;

/**
 * Created by ADMIN on 2017/11/2.
 */

@Service("mipwomtrlService")
public class MipwomtrlService implements IMipwomtrlService {
    private LogUtils logUtils;
    private static final String TX_NAME = "MIPWOMTRL";
    @Autowired
    private IRetWoMtrlDao retWoMtrlDao;
    @Autowired
    private IBisMtrlDao bisMtrlDao;
    
    @Override
    public String subMainProc(String evt_no, String strInMsg) {
        AppContext.clear();
        AppContext.setCurrServiceName(MipwomtrlService.class.getSimpleName());
        AppContext.setCurrEventNumber(evt_no);
        logUtils = new LogUtils(MipwomtrlService.class);
        logUtils.info("[InTrx:" + strInMsg + "]");
        String outMsg = "";

        MipwomtrlI inTrx = new MipwomtrlI();
        MipwomtrlO outTrx = new MipwomtrlO();

        outTrx.setTrx_id(TX_NAME);
        outTrx.setType_id(TRX_OUT);
        outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));

        try {
            inTrx = JacksonUtil.fromJson(strInMsg, MipwomtrlI.class);

            subMainProc2(inTrx.getAction_flg().toCharArray()[0],inTrx,outTrx);
            if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
            	retWoMtrlDao.flushCurSession();
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        } catch (Exception ex) {
            outTrx.setRtn_mesg(ex.toString());
            outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
            logUtils.error(Util.stackTraceToString(ex));
            TransactionAspectSupport.currentTransactionStatus()
                    .setRollbackOnly();
        } finally {
            outMsg = JacksonUtil.toJSONStr(outTrx);
            logUtils.info("[OutTrx:" + outMsg + "]");
        }
        return outMsg;
    }

    private void subMainProc2(char actionType, MipwomtrlI inTrx, MipwomtrlO outTrx) {

        switch (actionType) {
            case 'Q':{
                getbomByCondition(inTrx, outTrx);
                break;
            }

            default: {
                String gszMsgBuf = "Invalide type input: [" + actionType + "]";
                outTrx.setRtn_mesg(gszMsgBuf);
                outTrx.setRtn_code(String.valueOf(E_FIPWOMTRL_INVALID_INPUT));
            }
        }
    }

    private void getbomByCondition(MipwomtrlI inTrx, MipwomtrlO outTrx) {
        List<MipwomtrlOA> oary_bom = new ArrayList<MipwomtrlOA>();
        String sql = "FROM Ret_wo_mtrl WHERE wo_id_fk= '" + inTrx.getWo_id() + "'";
        
        if(!Util.isSpaceCheck(inTrx.getLine_id())){
        	sql += " AND line_id_fk = '" + inTrx.getLine_id() + "'";
        }
        if(!Util.isSpaceCheck(inTrx.getMtrl_prod_id())) {
            sql += " AND mtrl_prod_id_fk = '" + inTrx.getMtrl_prod_id() + "'";
		}
        if(!Util.isSpaceCheck(inTrx.getMtrl_cate())) {
            sql += " AND mtrl_cate = '" + inTrx.getMtrl_cate() + "'";
        }
        sql += " order by evt_timestamp asc";

		List<Ret_wo_mtrl> ret_wo_mtrls = retWoMtrlDao.find(sql);
        for(Ret_wo_mtrl ret_wo_mtrl : ret_wo_mtrls){
            MipwomtrlOA oaryA = new MipwomtrlOA();
            oaryA.setWo_id_fk(ret_wo_mtrl.getWo_id_fk());
            oaryA.setLine_id_fk(ret_wo_mtrl.getLine_id_fk());
            oaryA.setMtrl_cate(ret_wo_mtrl.getMtrl_cate());
            oaryA.setMtrl_prod_id_fk(ret_wo_mtrl.getMtrl_prod_id_fk());
            oaryA.setMtrl_lot_id(ret_wo_mtrl.getMtrl_lot_id());
            Bis_mtrl bis_mtrl = bisMtrlDao.get(ret_wo_mtrl.getMtrl_prod_id_fk());
            if(bis_mtrl == null){
                outTrx.setRtn_code(E_BIS_MTRL + E_READ_NOT_FOUND + "");
                outTrx.setRtn_mesg("未找到物料代码[" + ret_wo_mtrl.getMtrl_prod_id_fk() + "]的物料信息");
                return;
            }
            oaryA.setMtrl_unit(bis_mtrl.getMtrl_unit());
            oaryA.setMtrl_qty(ret_wo_mtrl.getMtrl_qty());
            oary_bom.add(oaryA);
        }
        outTrx.setOaryA(oary_bom);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
    }
}





